package com.bjsxt.service.impl;

import com.bjsxt.mapper.AdminMapper;
import com.bjsxt.pojo.Admin;
import com.bjsxt.pojo.PageA;
import com.bjsxt.service.AdminService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServiceImpl  implements AdminService {

    @Autowired
    private AdminMapper  adminMapper;

    @Override
    public Admin login(String aname, String apwd) {
        return adminMapper.selectOne(aname, apwd);
    }

    @Override
    public PageA<Admin> findAll(Integer page, Integer rows) {

        Page pg = PageHelper.startPage(page, rows);

        List<Admin> list = adminMapper.selectAll();

        PageA<Admin>   pga=new PageA<>(list,pg.getTotal());

        return pga;
    }

    @Override
    public Integer save(Admin admin,Integer rid) {
        Integer insert = 0;

        try {
            //添加管理员
            insert = adminMapper.insert(admin);

            //添加管理员对应的角色
            adminMapper.insertRole(admin.getAid(),rid);

            insert=1;
        } catch (Exception e) {
           throw  e;
        }

        return insert;
    }
}
